package ucap.com.lesanapp.activitys;

import android.app.Activity;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.ImageView;
import android.widget.Toast;

import com.google.gson.Gson;
import com.lidroid.xutils.HttpUtils;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.http.client.HttpRequest.HttpMethod;
import com.umeng.analytics.MobclickAgent;
import com.viewpagerindicator.TabPageIndicator;

import java.util.ArrayList;

import butterknife.Bind;
import butterknife.ButterKnife;
import ucap.com.lesanapp.R;
import ucap.com.lesanapp.base.BasePager;
import ucap.com.lesanapp.base.impl.AreaCountyBase;
import ucap.com.lesanapp.base.impl.TopNewsBase;
import ucap.com.lesanapp.base.impl.BranchBase;
import ucap.com.lesanapp.base.impl.FocusNewsBase;
import ucap.com.lesanapp.base.impl.LeaderBase;
import ucap.com.lesanapp.base.impl.TourBase;
import ucap.com.lesanapp.base.impl.VideoBase;
import ucap.com.lesanapp.bean.UpdateBean;
import ucap.com.lesanapp.global.Constants;
import ucap.com.lesanapp.view.UpDateDialog;


public class HomeActivity extends Activity {
    @Bind(R.id.vp_new_content)
    ViewPager vpContent;
    @Bind(R.id.tab_page)
    TabPageIndicator tabPager;
    @Bind(R.id.iv_seetings)
    ImageView ivSettings;
    ArrayList<BasePager> mList;
    private String[] titles = new String[]{
            "首页",
            "要闻",
            "领导",
            "视频",
            "旅游",
            "部门",
            "区县"

    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        this.requestWindowFeature(Window.FEATURE_NO_TITLE);//去掉标题栏
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_home);
        startActivity(new Intent(HomeActivity.this, SplashActivity.class));//启动闪屏页面
        ButterKnife.bind(this);
        intiView();
        version();
    }


    /**
     * 检查新版本
     */
    private void version() {
        HttpUtils utils = new HttpUtils();
        utils.send(HttpMethod.GET, Constants.URL_UPDATE, new RequestCallBack<String>() {
            @Override
            public void onSuccess(ResponseInfo<String> responseInfo) {
                Gson gson = new Gson();
                UpdateBean updateBean = gson.fromJson(responseInfo.result, UpdateBean.class);
                int version = Integer.parseInt(updateBean.getVersion());
                if (isUpdate(version)) {//检查是否又更新
                    UpDateDialog upDateDialog = UpDateDialog.showDialog(HomeActivity.this, updateBean.getVersion(), updateBean.getName(), updateBean.getSm(), updateBean.getPath());
                    upDateDialog.setCancelable(true);
                }
            }

            @Override
            public void onFailure(HttpException error, String msg) {
                Toast.makeText(HomeActivity.this, "连接错误！", Toast.LENGTH_LONG).show();
            }
        });
    }

    /**
     * h获取本地版本号
     *
     * @param code
     * @return
     */
    private boolean isUpdate(int code) {
        try {
            int packageCode = this.getPackageManager().getPackageInfo(this.getPackageName(), PackageManager.GET_CONFIGURATIONS).versionCode;
            if (packageCode < code) {
                return true;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return false;
    }


    private void intiView() {
        mList = new ArrayList<>();
        mList.add(new TopNewsBase(this));
        mList.add(new FocusNewsBase(this));
        mList.add(new LeaderBase(this));
        mList.add(new VideoBase(this));
        mList.add(new TourBase(this));
        mList.add(new BranchBase(this));
        mList.add(new AreaCountyBase(this));

        vpContent.setAdapter(new ContentAdapter());
        tabPager.setViewPager(vpContent);
        ivSettings.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(HomeActivity.this, SettingsActivity.class);
                startActivity(intent);
            }
        });

    }


    class ContentAdapter extends PagerAdapter {
        @Override
        public int getCount() {
            return mList.size();
        }

        @Override
        public boolean isViewFromObject(View view, Object object) {
            return view == object;
        }

        @Override
        public Object instantiateItem(ViewGroup container, int position) {
            BasePager basePager = mList.get(position);
            container.addView(basePager.mLayout);
            return basePager.mLayout;
        }

        @Override
        public void destroyItem(ViewGroup container, int position, Object object) {
            container.removeView((View) object);
        }

        @Override
        public CharSequence getPageTitle(int position) {
            return titles[position];
        }
    }

    @Override
    protected void onResume() {
        super.onResume();
        MobclickAgent.onResume(this);//友盟统计
    }

    public void onPause() {
        super.onPause();
        MobclickAgent.onPause(this);
    }
}
